<?php
/**
 * pc_menu.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-07 22:18
 * @modified 2018-07-07 22:18
 */

class ModelSettingPcMenu extends Model
{

    public function editMenu($data)
    {
        $menus = isset($data['menu']) ? $data['menu'] : array();
        $this->db->query("delete from " . DB_PREFIX . "menu");
        $this->db->query("delete from " . DB_PREFIX . "menu_description");

        if ($menus) {
            foreach ($menus as $menu) {
                $this->db->query("insert into " . DB_PREFIX . "menu set sort_order=" . (int)$menu['sort_order'] . ", href='" . $this->db->escape($menu['link']) . "', status=" . (int)$menu['status']);
                $menu_id = $this->db->getLastId();
                if ($menu['description']) {
                    foreach ($menu['description'] as $language_id => $description) {
                        $this->db->query("insert into " . DB_PREFIX . "menu_description set menu_id = " . (int)$menu_id . ", language_id=" . (int)$language_id . ", title = '" . $this->db->escape($description) . "'");
                    }
                }
            }
        }
    }
    public function getMenu()
    {
        $results = array();

        $menus = $this->db->query("select * from " . DB_PREFIX . "menu order by sort_order asc")->rows;
        if ($menus) {
            foreach ($menus as $menu) {
                $menu_id = $menu['menu_id'];
                $menu_description = $this->db->query("select * from " . DB_PREFIX . "menu_description where menu_id=" . (int)$menu_id)->rows;
                $descriptions = array();
                if ($menu_description) {
                    foreach ($menu_description as $item) {
                        $descriptions[$item['language_id']] = $item['title'];
                    }
                }
                $results[] = array(
                    'description' => $descriptions,
                    'link' => $menu['href'],
                    'sort_order' => $menu['sort_order'],
                    'status' => $menu['status']
                );
            }
        }

        return $results;
    }
}